//
//  MPAboutViewController_iPad.m
//  MissPaparazzi
//
//  Created by Alexey Aleshkov on 18.08.12.
//  Copyright (c) 2012 DzeTech. All rights reserved.
//


#import "MPAboutViewController.h"
#import "FXLabel.h"
#import "FXLabel+MPNavigationBarTitle.h"
#import "UIGlossyButton.h"
#import "UIGlossyButton+MPNavigationBarTitle.h"
#import "UIWebView+InnerShadow.h"


@interface MPAboutViewController ()

@end


@implementation MPAboutViewController

#pragma mark -
#pragma mark Init / Dealloc

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (!self) {
		return nil;
	}
	return self;
}

- (void)dealloc
{
	[_holePatternView release];
	[_webView release];
	[_shadowView release];
	[super dealloc];
}

#pragma mark -
#pragma mark Overrides

- (void)viewDidLoad
{
	[super viewDidLoad];
	
	NSString *titleViewLabelTitle = NSLocalizedString(@"AboutView.NavigationItem.Title", @"");
	FXLabel *titleViewLabel = [[FXLabel alloc] init];
	[titleViewLabel setMPNavigationBarTitleStyle];
	titleViewLabel.numberOfLines = 1;
	titleViewLabel.text = titleViewLabelTitle;
	titleViewLabel.font = [UIFont boldSystemFontOfSize:16];
	titleViewLabel.frame = CGRectMake(0, 0, 140, 44);
	titleViewLabel.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
	self.navigationItem.titleView = titleViewLabel;
	[titleViewLabel release];

	NSString *leftBarBackButtonTitle = NSLocalizedString(@"NavigationBar.BackButton.Title", @"");
	UIGNavigationButton *leftBarBackButton = [[UIGNavigationButton alloc] initWithFrame:CGRectMake(0, 0, 58, 32)];
	leftBarBackButton.leftArrow = YES;
	[leftBarBackButton addTarget:self action:@selector(backButtonTouchUpInsideAction:) forControlEvents:UIControlEventTouchUpInside];
	[leftBarBackButton setTitle:leftBarBackButtonTitle forState:UIControlStateNormal];
	[leftBarBackButton setMPNavigationBarTitleStyle];
	UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarBackButton];
	self.navigationItem.leftBarButtonItem = leftBarButtonItem;
	[leftBarButtonItem release];
	[leftBarBackButton release];
	
    _webView.scalesPageToFit = YES;
	_webView.innerShadowHidden = YES;
	
	NSBundle *mainBundle = [NSBundle mainBundle];
	NSString *aboutBundlePath = [mainBundle pathForResource:@"AboutPage" ofType:@"bundle"];
	NSBundle *aboutBundle = [NSBundle bundleWithPath:aboutBundlePath];
	NSString *aboutHTMLFilePath = [aboutBundle pathForResource:@"index" ofType:@"html" inDirectory:nil];
	NSURL *aboutHTMLFileURL = [NSURL fileURLWithPath:aboutHTMLFilePath];
	[_webView loadRequest:[NSURLRequest requestWithURL:aboutHTMLFileURL]];
	
	if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
	} else {
		_holePatternView.patternImage = [UIImage imageNamed:@"Pattern"];
		_holePatternView.holeInsets = UIEdgeInsetsMake(11, 16, 14, 16);
		_holePatternView.holeRadius = 6;
		_holePatternView.fillColor = [UIColor colorWithWhite:0 alpha:0.16];
		
		_shadowView.shadowInsets = _holePatternView.holeInsets;
		_shadowView.shadowRadius = _holePatternView.holeRadius;
		_shadowView.shadowBlur = 12;
	}
}

- (void)viewDidUnload
{
	[_holePatternView release];
	_holePatternView = nil;
	[_webView release];
	_webView = nil;
	[_shadowView release];
	_shadowView = nil;
	[super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	[self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	BOOL result;
	if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
		result = (UIInterfaceOrientationPortrait == interfaceOrientation);
	} else {
//		result = UIInterfaceOrientationIsLandscape(interfaceOrientation);
		result = YES;
	}
	// HACK: without this line view hides under nav bar O_O WHY?!
	//self.view.frame = self.view.frame;
	
	return result;
}

#pragma mark -
#pragma mark Actions

- (IBAction)backButtonTouchUpInsideAction:(id)sender
{
	[self.navigationController popViewControllerAnimated:YES];
}

@end
